package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonModel;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.api.CommonConfig;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.api.OrderApiService;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.FeedBackModel;
import com.tianhui.consignortwofei.mvp.model.enty.DriverDefaultInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.xuexiang.xui.widget.edittext.MultiLineEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/30
 * @description
 **/
public class DriverDefaultActivity extends DriverDefaultUpPictureActivity {
    @BindView(R.id.activity_feed_back_suggestEditText)
    MultiLineEditText mSuggestEditText;
    @BindView(R.id.activity_feed_back_contractInputItemView)
    InputItemView mContractInputItemView;
    @BindView(R.id.activity_feed_back_recyclerView)
    RecyclerView mRecyclerView;
    private FeedBackModel mModel;
    private DriverDefaultInfo mInfo;
    private OrderInfo mOrderInfo;
    private String dispatchno;

    @Override
    public int getLayoutId() {
        return R.layout.activity_driver_default;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarTitle() {
        return "司机违约";
    }

    @Override
    public void init() {
        super.init();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            mOrderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            if (mOrderInfo != null) {
//                int orderType = bundle.getInt(KeyValue.KEY_TYPE);
                dispatchno = mOrderInfo.dispatchno;

            } else {
                Toast.makeText(DriverDefaultActivity.this, "订单错误", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    public RecyclerView getPictureRecyclerView() {
        return mRecyclerView;
    }

    @Override
    public String getPictureModule() {
        return "feedback";
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }


    @OnClick(R.id.activity_feed_back_commitButton)
    public void commit() {
        String content = mSuggestEditText.getContentText();
//        String contact = mContractInputItemView.getContent();
        if (TextUtils.isEmpty(content)) {
            showToast("违约描述不能为空");
            return;
        }
//        if (TextUtils.isEmpty(contact)) {
//            showToast("联系方式不能为空");
//            return;
//        }

        if (mInfo == null) {
            mInfo = new DriverDefaultInfo();
        }
//        mInfo.content = content;

        String selectPictureId = getSelectPictureId();
        if (TextUtils.isEmpty(selectPictureId)) {
            showToast("违约图片不能为空");
            return;
        }
//        mInfo.picture = selectPictureId;
//
//        mInfo.terminal = "Android";
//        mInfo.feedtype = "1";
//        mInfo.occtype = "1";
//        mInfo.driverid = SharedPreferenceManager.getInstance().getConsignorId();

        Map<String, String> map = new HashMap<>();
        map.put("dispatchno", dispatchno);
        map.put("deductionreason", content);
        map.put("deductionimage", selectPictureId);
//        mInfo.dispatchno=dispatchno;
//        mInfo.deductionreason=content;
//        mInfo.deductionimage=selectPictureId;

//        LogUtil.print(mInfo.toString());
        requestFeedBackCommit(map);
    }

    private void requestFeedBackCommit(Map<String, String> map) {
//        if (mModel == null) {
//            mModel = new FeedBackModel();
//        }

//        mModel.driverDefaultCommitModel(this, mInfo, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
//            @Override
//            public void onNext(CommonResponse baseResponse) {
//                if (baseResponse.getCode() == 20000) {
//                    showToast("提交成功");
//                    finish();
//                } else {
//                    showToast(baseResponse.getMsg());
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//
//                showToast(e.message);
//
//            }
//        });


        new CommonModel().subscribe(this, CommonConfig.getInstance().create(OrderApiService.class).driverDefaultCommit(map), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
//                    if (context != null) {
//                        ((Activity) context).finish();
//                    }
//                    EventBusManage.operateActivityFinish();
//                    EventBusManage.refreshOrderDetail();
//                    EventBusManage.refreshOrderList();
//                    Bundle bundle = new Bundle();
//                    bundle.putParcelable(KeyValue.KEY_ORDER_INFO, orderInfo);
//                    bundle.putInt(KeyValue.KEY_TYPE, OrderType.TYPE_ADJUST_WEIGHT);
//                    bundle.putString(KeyValue.KEY_VALUE, adjustInfo.adjustValue);
//                    Intent intent = new Intent(context, AdjustSuccessActivity.class);
//                    intent.putExtras(bundle);
//                    context.startActivity(intent);
                    ToastUtil.showShortToast("已提交，请等待审核");
                    finish();
                } else {
                    ToastUtil.showShortToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                ToastUtil.showShortToast(e.message);
            }
        });

    }


    @Override
    protected boolean isShowFinishTipDialog() {
        return true;
    }
}
